﻿using System.IO;
using System.Security.Cryptography;
using UnityEngine;

public static class SMD5Util
{
    public static Hash128 File(string file)
    {
        try
        {
            FileStream fs = new FileStream(file, FileMode.Open);
            MD5 md5 = new MD5CryptoServiceProvider();
            byte[] bytes = md5.ComputeHash(fs);
            fs.Close();
            return new Hash128(
                MakeUint(bytes[0], bytes[1], bytes[2], bytes[3]),
                MakeUint(bytes[4], bytes[5], bytes[6], bytes[7]),
                MakeUint(bytes[8], bytes[9], bytes[10], bytes[11]),
                MakeUint(bytes[12], bytes[13], bytes[14], bytes[15]));
        }
        catch
        {
            return new Hash128();
        }
    }

    static uint MakeUint(byte b1, byte b2, byte b3, byte b4)
    {
        return b1 + (uint)(b2 << 8) + (uint)(b3 << 16) + (uint)(b4 << 24);
    }
}